
$(function () {
    initConfigs();

    $("#mail-config .btn-save").click(function () {
    	saveConfigs()
    });
    
//    $("#mail-config .btn-close").click(function () {
//    	$.post(ctx + "smtp/sendmail", {}, function(data){
//    		console.log("邮件发送完成");
//    		console.log(data);
//    	});
//    });

});

function initConfigs() {
	
    $.post(ctx + "smtp/settings", {}, function (data) {
	    if($.trim(data.OPEN_MAIL_ALERT)=='Off'){
	    	$('input:radio[value="0"]').attr('checked', 'true');
	    }else if('SMTP' == data.OPEN_MAIL_ALERT){
	    	$('input:radio[value="1"]').attr('checked', 'true');
	    }
        $("[name='email-address']").val(data.FROM_MAIL_ADDRESS);
        $("[name='sending-name']").val(data.FROM_MAIL_NICKNAME);
        $("[name='admin-address']").val(data.ADMIN_MAIL_ADDRESS);
        $("[name='smtp-host']").val(data.SMTP_HOST);
        $("[name='smtp-port']").val(data.SMTP_PORT);
        $("[name='smtp-username']").val(data.SMTP_USERNAME);
        $("[name='smtp-password']").val(data.SMTP_PWD);
        
        if(data.SMTP_SECRET_KEY == ''){
        	$('input:radio[value="5"]').attr('checked', 'true');
        }else if('SSL' == $.trim(data.SMTP_SECRET_KEY)){
	    	$('input:radio[value="6"]').attr('checked', 'true');
	    }else if('TLS' == $.trim(data.SMTP_SECRET_KEY)){
	    	$('input:radio[value="7"]').attr('checked', 'true');
	    }else{
	    	$('input:radio[value="5"]').attr('checked', 'true');
	    }
    });
}

function saveConfigs(){

	$isUseMailAlert = $('input[name="email_alert_selection"]:checked').val();
	$mailFromAddress = $("[name='email-address']").val();
	$mailFromNickname = $("[name='sending-name']").val();
	$adminMailAddress = $("[name='admin-address']").val();
	$smtpHost = $("[name='smtp-host']").val();
	$smtpPort = $("[name='smtp-port']").val();
	$smtpUserName = $("[name='smtp-username']").val();
	$smtpPwd = $("[name='smtp-password']").val();
	$secretKey = $('input[name="smtp-secure"]:checked').val();
	
//	console.log("isAlert = "+$isUseMailAlert+"\r\nfromAddress = "+$mailFromAddress+"\r\nNickName = "+$mailFromNickname+"\r\nAdminAddress = "+$adminMailAddress
//			+"\r\nHost = "+$smtpHost+"\r\nPort = "+$smtpPort+"\r\nsmtpUserName = "+$smtpUserName+"\r\nsmtpPwd = "+$smtpPwd);
	
	var tips_edit_menu = $MB.i18n('vali.email.msg2');
	// 验证格式并提示
	var isEmail = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	if($mailFromAddress !== '' && $mailFromAddress!== 'undefined' && !(isEmail.test($mailFromAddress))){
		$MB.n_danger(tips_edit_menu);
		return;
	}
	if($adminMailAddress !== '' && $adminMailAddress!== 'undefined' && !(isEmail.test($adminMailAddress))){
		$MB.n_danger(tips_edit_menu);
		return;
	}
	
	$reqData = {"openMailAlert":($isUseMailAlert==null?"":$isUseMailAlert), 
				"fromMailAddress":($mailFromAddress==null?"":$mailFromAddress),
				"fromMailNickname":($mailFromNickname==null?"":$mailFromNickname),
				"adminMailAddress":($adminMailAddress==null?"":$adminMailAddress),
				"smtpHost":($smtpHost==null?"":$smtpHost),
				"smtpPort":($smtpPort==null?"":$smtpPort),
				"smtpUsername":($smtpUserName==null?"":$smtpUserName),
				"smtpPwd":($smtpPwd==null?"":$smtpPwd),
				"secretKey":($secretKey==null?"":$secretKey)
				};
	$.post(ctx + "smtp/save", $reqData, function (r) {
        var data = r;
        $MB.n_success($MB.i18n('operation.success'));
    });
}
